/**
* Script to stem concept names and synonyms from a previosly
* filtered tbl file (see FilterTblFile.groovy). 
*
* @param	stemmer	name of the stemmer from Snowball
*			which is then used for stemming
* @param	file	filtered tbl file, of which content
*			is  now to be stemmed
* @author	Anika Oellrich
*/

import org.tartarus.snowball.*;

public void stemTokens(StringBuffer _sb, String[] _tokens, SnowballStemmer _stemmer) {
  _tokens.each { t ->
    if ( !t.equals("") ) {
      _stemmer.setCurrent( t.toLowerCase() )
      _stemmer.stem()
      _sb.append(" ")
      _sb.append( _stemmer.getCurrent() )
    }
  }
}

Class stemClass = Class.forName("org.tartarus.snowball.ext." + args[0] + "Stemmer");
SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance();

new File(args[1]).splitEachLine("\t") { splits ->
  if ( splits.size() > 1 ) {
    def pmid = splits[0].trim()
    def sb = new StringBuffer()

    // stem tokens in concept name
    stemTokens(sb, splits[1].trim().split(" "), stemmer)
    
    // if synonyms, stem those as well
    if (splits.size() == 3) {
      def synonyms = splits[2].split("\\|")
      
      sb.append("\t")
      synonyms.each { s ->
        stemTokens(sb, s.trim().split(" "), stemmer)
        sb.append("|")
      }
      
    }

    if ( splits.size() == 3) println pmid + "\t" + sb.substring(0, sb.length() - 1)
    else println pmid + "\t" + sb.toString()
  }
}
